package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.beardedhen.androidbootstrap.AwesomeTextView;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.FileOutputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.event.CluelistSyncEvent;
import palmtop.com.palmtop.event.TaskdetailSyncEvent;
import palmtop.com.palmtop.model.Taskgrid;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.FuncUtil;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.ToastUtil;

public class TaskdetailActivity extends BaseActivity {

    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.taskname)
    AwesomeTextView taskname;
    @BindView(R.id.taskaddress)
    AwesomeTextView taskaddress;
    @BindView(R.id.taskcontent)
    AwesomeTextView taskcontent;
    @BindView(R.id.adddate)
    AwesomeTextView adddate;
    /*@BindView(R.id.addusername)
    AwesomeTextView addusername;*/
    //addusername
    @BindView(R.id.footBtn)
    Button footBtn;

    Taskgrid taskgrid;
    @BindView(R.id.zphoto1)
    ImageView zphoto1;
    @BindView(R.id.zphoto2)
    ImageView zphoto2;
    @BindView(R.id.zphoto3)
    ImageView zphoto3;
    @BindView(R.id.zphoto4)
    ImageView zphoto4;
    @BindView(R.id.lookmapBtn)
    Button lookmapBtn;

    MyApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_taskdetail2, Config.toptitle, 1);
        ButterKnife.bind(this);
        app = (MyApplication) getApplication();
        app.addActivity(TaskdetailActivity.this);
        app.addActivityList(TaskdetailActivity.this);
        initview();
    }

    private void initview() {
        mtitle.setText("任务详情");
        footBtn.setText("签  收");
        footBtn.setVisibility(View.GONE);
        taskgrid = (Taskgrid) getIntent().getSerializableExtra("taskgrid");
        if (taskgrid != null) {
            if(taskgrid.getStatus().equals("0")){
                footBtn.setVisibility(View.VISIBLE);
            }
            taskname.setText(taskgrid.getTaskname());
            taskaddress.setText(taskgrid.getTaskaddress());
            taskcontent.setText(taskgrid.getTaskcontent());
            adddate.setText(taskgrid.getAdddate());
//            addusername.setText(taskgrid.getAddusername());
            if (taskgrid.getPhoto1() != null && !taskgrid.getPhoto1().equals("")) {
                x.image().bind(zphoto1, Config.IMG_URL + taskgrid.getPhoto1());
                FuncUtil.clickShowBigPic(Config.IMG_URL + taskgrid.getPhoto1(), zphoto1, TaskdetailActivity.this);
            }
            if (taskgrid.getPhoto2() != null && !taskgrid.getPhoto2().equals("")) {
                x.image().bind(zphoto2, Config.IMG_URL + taskgrid.getPhoto2());
                FuncUtil.clickShowBigPic(Config.IMG_URL + taskgrid.getPhoto2(), zphoto2, TaskdetailActivity.this);
            }
            if (taskgrid.getPhoto3() != null && !taskgrid.getPhoto3().equals("")) {
                x.image().bind(zphoto3, Config.IMG_URL + taskgrid.getPhoto3());
                FuncUtil.clickShowBigPic(Config.IMG_URL + taskgrid.getPhoto3(), zphoto3, TaskdetailActivity.this);
            }
            if (taskgrid.getPhoto4() != null && !taskgrid.getPhoto4().equals("")) {
                x.image().bind(zphoto4, Config.IMG_URL + taskgrid.getPhoto4());
                FuncUtil.clickShowBigPic(Config.IMG_URL + taskgrid.getPhoto4(), zphoto4, TaskdetailActivity.this);
            }

        }
    }

    @OnClick(R.id.footBtn)
    public void onViewClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskgridapp/updateStatus");
        params.addBodyParameter("id", String.valueOf(taskgrid.getId()));
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    ToastUtil.show(TaskdetailActivity.this, "签收成功");
                    //TaskdetailSyncEvent
                    EventBus.getDefault().post(new TaskdetailSyncEvent());
                    finish();

                } else {
                    ToastUtil.show(TaskdetailActivity.this, "签收失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    @OnClick(R.id.lookmapBtn)
    public void onlookmapClicked() {
        Intent i = new Intent();
        if(TextUtils.isEmpty(taskgrid.getLatitude())){
            i.putExtra("latitude",taskgrid.getLatitude());
        }
        if(TextUtils.isEmpty(taskgrid.getLongitude())){
            i.putExtra("longitude",taskgrid.getLongitude());
        }
        i.setClass(this, LookmapActivity.class);
        startActivityForResult(i,1);
//        startActivityForResult(new Intent(AddClueActivity.this, SelectPositionActivity.class), 5);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
